package com.frxuexi.park2212.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/LoginActivity")
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding,UserViewModel>() {
    override fun initData() {
        vm.loginSuc.observe(this){
            ToastUtils.showLong("登录成功")
            SPUtils.getInstance().apply {
                put(Const.TOKEN,it.token)
                put(Const.USERNAME,it.username)
                put(Const.UID,it.id)
                put(Const.ROLE,it.roleid)
            }
//            if(it.roleid==2) {
                ARouter.getInstance().build("/app/MainActivity").navigation()
//            }else{
//                ARouter.getInstance().build("/app/WebViewActivity").navigation()
//            }
            finish()
        }
    }

    override fun initView() {
        val token = SPUtils.getInstance().getString(Const.TOKEN, "")
        if(token.isNotEmpty()){
            ARouter.getInstance().build("/app/MainActivity").navigation()
            finish()
        }

        binding.btnLogin.setOnClickListener {
            val uname = binding.loginUname.text.toString()
            val upass = binding.loginPass.text.toString()
            if(uname.isNullOrEmpty() || upass.isNullOrEmpty()){
                ToastUtils.showLong("输入不能为空")
                return@setOnClickListener
            }
            if(!uname.matches(Regex("[A-z0-9_]{2,8}"))){
                ToastUtils.showLong("不是合法账号")
                return@setOnClickListener
            }
            vm.login(uname,upass)
        }
    }

}